﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Tools;
using WPF数据采集系统.Model;
using WPF数据采集系统.ViewModel;

namespace WPF数据采集系统.DAL
{
    public class DataSql
    {
        string dbConfig = ConfigurationManager.ConnectionStrings["db_config"].ToString();
        public DataSql()
        {
            SqlHelper.connString = dbConfig;
        }
        public List<PlcModel> GetPlcInfo()
        {
            try
            {
                string sql = "select * from PlcInfo order by plc_id ASC";
                List<PlcModel> list = new List<PlcModel>();
                SqlDataReader result = SqlHelper.GetReader(sql);
                if (result.HasRows)
                {
                    while (result.Read())
                    {
                        PlcModel pm = new PlcModel();
                        pm.PlcId = (int)result["plc_id"];
                        pm.WriteType = result["plc_write_type"].ToString();
                        pm.WriteData = result["plc_write_data"].ToString();
                        pm.WriteLength = (int)result["plc_write_length"];
                        pm.WriteAddress = (int)result["plc_write_address"];
                        pm.ReadLength = (int)result["plc_read_length"];
                        pm.ReadType = result["plc_read_type"].ToString();
                        pm.ReadAddress = (int)result["plc_read_address"];
                        pm.PlcPort = (int)result["plc_port"];
                        pm.PlcType = result["plc_type"].ToString();
                        pm.PlcIp = result["plc_ip"].ToString();
                        pm.LocalIp = result["plc_local_ip"].ToString();
                        pm.LocalPort = (int)result["plc_local_port"];
                        pm.AutoConnect = Convert.ToBoolean(result["plc_auto_connect"]);
                        pm.Usability = Convert.ToBoolean(result["plc_usability"]);
                        pm.Core = Convert.ToBoolean(result["plc_core"]);
                        list.Add(pm);
                    }
                }
                return list;
            }
            catch(Exception ex)
            {
                throw ex;
            }

        }
        public List<PlcMonitorInfo> GetPlcMonitorInfo()
        {
            try
            {
                string sql = "select * from PlcMonitorInfo order by address ASC";
                List<PlcMonitorInfo> list = new List<PlcMonitorInfo>();
                SqlDataReader result = SqlHelper.GetReader(sql);
                if (result.HasRows)
                {
                    while (result.Read())
                    {
                        PlcMonitorInfo pmi = new PlcMonitorInfo();
                        pmi.address = (int)result["address"];
                        pmi.length = (int)result["length"];
                        pmi.display = Convert.ToBoolean(result["display"]);
                        pmi.sort = (int)result["sort"];
                        pmi.name = (string)result["name"];
                        pmi.type = (string)result["type"];
                        pmi.enable = Convert.ToBoolean(result["enable"]);
                        pmi.data_type = (string)result["data_type"];
                        list.Add(pmi);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public DataTable GetTesterData()
        {
            try
            {
                string sql = "select top 2* from TestResult order by 日期 DESC";
                return SqlHelper.GetDataSet(sql).Tables[0];
            }
            catch(Exception ex)
            {
                throw ex;
            }

        }
        public DataTable GetUsersData()
        {
            try
            {
                string sql = "select * from Users order by id";
                return SqlHelper.GetDataSet(sql).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public bool SavePlcData(PlcModel pm)
        {
            try
            {
                string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                PlcTool.IniWrite("OmronPLC", "PLC IP地址", pm.PlcIp, path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "本地IP地址", pm.LocalIp, path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "PLC地址类型", pm.PlcType, path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "本机端口号", pm.LocalPort.ToString(), path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "PLC端口号", pm.PlcPort.ToString(), path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "读取地址", pm.ReadAddress.ToString(), path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "读取长度", pm.ReadLength.ToString(), path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "读取类型", pm.ReadType, path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "写入地址", pm.WriteAddress.ToString(), path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "写入长度", pm.WriteLength.ToString(), path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "写入数据", pm.WriteData, path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "写入类型", pm.WriteType, path + "\\OmronPLCAddress.ini");
                PlcTool.IniWrite("OmronPLC", "自动连接", pm.AutoConnect.ToString(), path + "\\OmronPLCAddress.ini");
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

    }
}
